package com.novellotus.iov.carvice;

import android.annotation.TargetApi;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.multidex.MultiDex;

import com.alipay.sdk.app.EnvUtils;
import com.mob.MobSDK;
import com.mob.tools.proguard.ProtectedMemberKeeper;
import com.novellotus.iov.carvice.utils.MyUtils;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.beta.interfaces.BetaPatchListener;
import com.tencent.tinker.loader.app.DefaultApplicationLike;

import java.util.Locale;

/**
 * Created by Rich on 2017/12/21.
 *
 */

public class MyApplicationLike extends DefaultApplicationLike  implements ProtectedMemberKeeper {

    public MyApplicationLike(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent) {
        super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent);
    }


    @Override
    public void onCreate() {
        super.onCreate();
        //开启支付宝沙箱测试
        EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
       // Beta.checkUpgrade();
        // 设置是否开启热更新能力，默认为true
        Beta.enableHotfix = true;
        // 设置是否自动下载补丁，默认为true
        Beta.canAutoDownloadPatch = true;
        // 设置是否自动合成补丁，默认为true
        Beta.canAutoPatch = true;
        // 设置是否提示用户重启，默认为false
        Beta.canNotifyUserRestart = true;
        // 补丁回调接口
        Beta.betaPatchListener = new BetaPatchListener() {
            @Override
            public void onPatchReceived(String patchFile) {
                MyUtils.logInformation("补丁下载地址" + patchFile);
            }

            @Override
            public void onDownloadReceived(long savedLength, long totalLength) {
                MyUtils.logInformation( String.format(Locale.getDefault(), "%s %d%%",
                        Beta.strNotificationDownloading,
                        (int) (totalLength == 0 ? 0 : savedLength * 100 / totalLength)));
            }

            @Override
            public void onDownloadSuccess(String msg) {
                MyUtils.logInformation("补丁下载成功");
            }

            @Override
            public void onDownloadFailure(String msg) {
                MyUtils.logInformation("补丁下载失败");

            }

            @Override
            public void onApplySuccess(String msg) {
                MyUtils.logInformation("补丁应用成功");
            }

            @Override
            public void onApplyFailure(String msg) {
                MyUtils.logInformation("补丁应用失败");
            }

            @Override
            public void onPatchRollback() {

            }
        };
        Bugly.init(getApplication(), "f14d758e82", false);
        //定义开发设备   true为开发设备 false为用户设备
        Bugly.setIsDevelopmentDevice(getApplication(), false);
        // 这里实现SDK初始化，appId替换成你的在Bugly平台申请的appId
        // 调试时，将第三个参数改为true

    }


    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    public void onBaseContextAttached(Context base) {
        super.onBaseContextAttached(base);
        // you must install multiDex whatever tinker is installed!
        MultiDex.install(base);
        // 安装tinker
        Beta.installTinker(this);
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public void registerActivityLifecycleCallback(Application.ActivityLifecycleCallbacks callbacks) {
        getApplication().registerActivityLifecycleCallbacks(callbacks);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        Beta.unInit();
    }
}
